/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BroadFrame.java
 *
 * Created on 2011-11-5, 12:18:07
 */

package com.lanchat.view;

import com.lanchat.model.data.ChatMessage;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.LinkedList;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/**
 *
 * @author GM
 */
public class BroadFrame extends javax.swing.JFrame {

    /** Creates new form BroadFrame */
    public BroadFrame() {
        initComponents();
    }

    public BroadFrame(LinkedList<String> list){
        initComponents();
        UserPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        for(int i=0;i<list.size();i++){
            JCheckBox tmp=new JCheckBox(list.get(i));
            System.out.println(list.get(i));
            UserPanel.add(tmp);
        }
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        UserPanel = new javax.swing.JPanel();
        SendText = new javax.swing.JTextField();
        SendButton = new javax.swing.JButton();
        CloseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        UserPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("用户"));

        javax.swing.GroupLayout UserPanelLayout = new javax.swing.GroupLayout(UserPanel);
        UserPanel.setLayout(UserPanelLayout);
        UserPanelLayout.setHorizontalGroup(
            UserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 313, Short.MAX_VALUE)
        );
        UserPanelLayout.setVerticalGroup(
            UserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 206, Short.MAX_VALUE)
        );

        SendButton.setText("发送");
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        CloseButton.setText("关闭");
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(UserPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(SendText, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SendButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CloseButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(UserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SendButton)
                    .addComponent(CloseButton))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        // TODO add your handling code here:
        if(SendText.getText().length()==0){
            JOptionPane.showMessageDialog(null, "请输入消息！");
            return;
        }

        Component[] tmp=UserPanel.getComponents();
        LinkedList<String> tmpList=new LinkedList<String>();
        for(int i=0;i<tmp.length;i++){
            if(((AbstractButton)tmp[i]).isSelected()==true){
                tmpList.add((String)(((JCheckBox)tmp[i]).getText()));
            }
        }

        if(tmpList.size()==0){
            JOptionPane.showMessageDialog(null, "请选择用户！");
            return;
        }
        ChatMessage cm=new ChatMessage(SendText.getText()+"（群发）", MainListFrame.localUser.getUserName(),
                    MainListFrame.localUser.getAddress(), ChatMessage.MSG);
        for(int i=0;i<tmpList.size();i++){
            String ip=tmpList.get(i).substring(tmpList.get(i).indexOf(':')+1);
            MainListFrame.send.send(cm, ip);
        }
    }//GEN-LAST:event_SendButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
       LinkedList<String> list=new LinkedList<String>();
       list.add("123");
       list.add("456");
       new BroadFrame(list);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CloseButton;
    private javax.swing.JButton SendButton;
    private javax.swing.JTextField SendText;
    private javax.swing.JPanel UserPanel;
    // End of variables declaration//GEN-END:variables

}
